package Arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _229_MajorityElementII {
    public List<Integer> majorityElement(int[] nums) {
        int length = nums.length;
        int threshold = length/3;
        List<Integer> results = new ArrayList<Integer>();
        if(length==0) return results;
        Arrays.sort(nums);
        System.out.println(nums.toString());
        int count = 0;
        int temp = nums[0];
        for(int i=0;i<length;i++){
            if(temp==nums[i]){
                count++;
            }else{
                if(count>threshold){
                    results.add(temp);
                    count = 1;
                    temp = nums[i];
                }else{
                    count = 1;
                    temp = nums[i];
                }
            }
        }
        if(count>threshold){
            results.add(temp);
        }
        return results;
    }
}
